import Mock from 'mockjs2'
import { builder } from '../util'

const text = 'Day\tCountry\tGtTask\tClick\tImpr\tRevenue\n' +
  '2020-02-25\t🇮🇳IND\t145,115,951\t110,916,407\t7,640,477\t7,196.14\n' +
  '2020-02-25\t🇮🇩IDN\t45,574,761\t36,408,475\t3,072,608\t5,073.65\n' +
  '2020-02-25\t🇺🇸USA\t21,873,976\t15,229,105\t6,236,509\t3,548.77\n' +
  '2020-02-25\t🇲🇽MEX\t13,211,930\t4,892,507\t8,982,116\t1,428.46\n' +
  '2020-02-24\t🇮🇳IND\t140,176,983\t109,617,329\t7,147,725\t6,254.61\n' +
  '2020-02-24\t🇮🇩IDN\t44,165,549\t35,906,126\t3,325,966\t4,106.80\n' +
  '2020-02-24\t🇺🇸USA\t18,362,980\t13,434,443\t4,514,845\t2,882.68\n' +
  '2020-02-24\t🇲🇽MEX\t12,934,010\t4,569,622\t9,071,097\t2,222.36\n' +
  '2020-02-23\t🇮🇳IND\t140,231,813\t111,075,607\t7,294,386\t8,307.42\n' +
  '2020-02-23\t🇮🇩IDN\t34,705,430\t27,259,410\t2,997,589\t2,878.90\n' +
  '2020-02-23\t🇺🇸USA\t18,503,950\t13,807,408\t4,192,886\t3,704.77\n' +
  '2020-02-23\t🇲🇽MEX\t13,166,985\t4,789,779\t9,039,060\t2,134.00\n' +
  '2020-02-22\t🇮🇳IND\t135,773,068\t105,882,305\t7,525,759\t7,220.00\n' +
  '2020-02-22\t🇮🇩IDN\t36,253,762\t29,090,013\t2,861,429\t3,729.45\n' +
  '2020-02-22\t🇺🇸USA\t17,705,161\t12,965,158\t4,228,584\t5,201.52\n' +
  '2020-02-22\t🇲🇽MEX\t13,320,272\t4,559,993\t9,459,514\t1,917.58\n' +
  '2020-02-21\t🇮🇳IND\t136,535,859\t102,208,234\t8,454,760\t7,527.60\n' +
  '2020-02-21\t🇮🇩IDN\t39,678,234\t32,345,933\t2,691,547\t4,261.56\n' +
  '2020-02-21\t🇺🇸USA\t22,824,435\t17,800,467\t4,457,042\t5,534.05\n' +
  '2020-02-21\t🇲🇽MEX\t13,409,654\t4,483,261\t9,653,636\t1,404.23\n' +
  '2020-02-20\t🇮🇳IND\t136,859,743\t102,603,865\t11,941,118\t7,577.88\n' +
  '2020-02-20\t🇮🇩IDN\t47,007,138\t38,914,801\t2,414,225\t4,274.26\n' +
  '2020-02-20\t🇺🇸USA\t23,020,266\t18,249,585\t4,096,930\t5,501.16\n' +
  '2020-02-20\t🇲🇽MEX\t13,491,271\t4,603,791\t9,604,455\t1,658.48\n' +
  '2020-02-19\t🇮🇳IND\t137,617,331\t102,168,879\t13,627,767\t6,566.07\n' +
  '2020-02-19\t🇮🇩IDN\t46,070,759\t38,400,638\t2,248,965\t3,815.13\n' +
  '2020-02-19\t🇺🇸USA\t19,567,670\t15,640,066\t3,370,872\t5,492.13\n' +
  '2020-02-19\t🇲🇽MEX\t12,548,142\t3,812,200\t9,492,692\t1,539.36\n' +
  '2020-02-18\t🇮🇳IND\t127,224,686\t89,859,790\t13,322,161\t6,633.00\n' +
  '2020-02-18\t🇮🇩IDN\t44,836,883\t36,745,309\t2,057,463\t4,354.51\n' +
  '2020-02-18\t🇺🇸USA\t18,440,831\t14,837,712\t3,134,596\t4,779.57\n' +
  '2020-02-18\t🇲🇽MEX\t13,125,800\t4,395,165\t9,445,354\t1,530.74\n' +
  '2020-02-17\t🇮🇳IND\t121,656,218\t91,283,565\t12,048,863\t6,465.58\n' +
  '2020-02-17\t🇮🇩IDN\t42,150,098\t35,695,889\t1,137,483\t4,794.91\n' +
  '2020-02-17\t🇺🇸USA\t15,059,877\t11,600,073\t3,149,517\t2,475.05\n' +
  '2020-02-17\t🇲🇽MEX\t13,211,934\t4,401,839\t9,564,545\t2,296.30\n' +
  '2020-02-16\t🇮🇳IND\t123,669,720\t90,962,195\t11,607,447\t7,540.78\n' +
  '2020-02-16\t🇮🇩IDN\t45,907,887\t33,997,669\t4,832,569\t3,592.99\n' +
  '2020-02-16\t🇺🇸USA\t12,556,088\t9,118,160\t3,131,837\t2,315.00\n' +
  '2020-02-16\t🇲🇽MEX\t12,614,764\t3,832,971\t9,546,649\t2,326.20\n' +
  '2020-02-15\t🇮🇳IND\t124,857,270\t86,650,791\t11,463,008\t6,785.35\n' +
  '2020-02-15\t🇮🇩IDN\t45,403,052\t31,794,049\t5,274,052\t3,486.68\n' +
  '2020-02-15\t🇺🇸USA\t11,474,344\t8,029,226\t3,106,171\t2,787.00\n' +
  '2020-02-15\t🇲🇽MEX\t12,020,812\t3,464,574\t9,313,633\t2,813.70\n' +
  '2020-02-14\t🇮🇳IND\t138,214,480\t99,207,055\t12,377,958\t6,779.10\n' +
  '2020-02-14\t🇮🇩IDN\t50,077,553\t35,291,577\t7,793,120\t4,189.22\n' +
  '2020-02-14\t🇺🇸USA\t8,566,708\t5,684,754\t2,636,847\t1,984.80\n' +
  '2020-02-14\t🇲🇽MEX\t11,584,623\t3,176,932\t9,187,677\t1,443.94\n' +
  '2020-02-13\t🇮🇳IND\t136,191,871\t105,229,572\t11,795,545\t6,897.00\n' +
  '2020-02-13\t🇮🇩IDN\t51,755,012\t34,041,788\t11,432,404\t4,739.75\n' +
  '2020-02-13\t🇺🇸USA\t10,931,154\t7,553,713\t3,109,303\t1,458.59\n' +
  '2020-02-13\t🇲🇽MEX\t12,164,299\t3,534,352\t9,434,949\t1,268.48\n' +
  '2020-02-12\t🇮🇳IND\t126,862,783\t95,561,930\t12,427,463\t7,100.63\n' +
  '2020-02-12\t🇮🇩IDN\t48,638,578\t31,817,267\t10,782,173\t4,857.14\n' +
  '2020-02-12\t🇺🇸USA\t11,527,047\t8,421,000\t2,836,980\t1,606.43\n' +
  '2020-02-12\t🇲🇽MEX\t12,488,425\t3,721,793\t9,572,534\t1,184.35\n' +
  '2020-02-11\t🇮🇳IND\t112,656,099\t85,361,659\t14,693,011\t7,726.81\n' +
  '2020-02-11\t🇮🇩IDN\t47,746,445\t31,394,750\t10,663,172\t4,911.17\n' +
  '2020-02-11\t🇺🇸USA\t11,183,176\t8,377,573\t2,527,750\t1,741.35\n' +
  '2020-02-11\t🇲🇽MEX\t11,642,356\t2,975,213\t9,500,521\t1,222.66\n' +
  '2020-02-10\t🇮🇳IND\t109,902,806\t82,093,464\t15,840,616\t7,100.24\n' +
  '2020-02-10\t🇮🇩IDN\t46,365,805\t31,265,725\t9,964,419\t4,606.88\n' +
  '2020-02-10\t🇺🇸USA\t11,218,445\t8,543,982\t2,439,396\t1,481.75\n' +
  '2020-02-10\t🇲🇽MEX\t11,783,226\t2,908,810\t9,748,822\t2,054.40\n' +
  '2020-02-09\t🇮🇳IND\t111,990,815\t81,330,017\t15,119,169\t8,093.26\n' +
  '2020-02-09\t🇮🇩IDN\t45,996,120\t30,849,239\t9,996,523\t3,701.82\n' +
  '2020-02-09\t🇺🇸USA\t11,438,033\t8,818,581\t2,323,391\t1,092.46\n' +
  '2020-02-09\t🇲🇽MEX\t12,492,673\t3,586,872\t9,727,728\t1,986.47\n' +
  '2020-02-08\t🇮🇳IND\t111,100,221\t82,356,688\t14,631,738\t7,789.01\n' +
  '2020-02-08\t🇮🇩IDN\t46,513,561\t31,673,171\t9,456,303\t3,994.35\n' +
  '2020-02-08\t🇺🇸USA\t12,020,756\t9,059,006\t2,680,221\t1,738.95\n' +
  '2020-02-08\t🇲🇽MEX\t12,748,205\t3,973,458\t9,532,571\t1,519.74\n' +
  '2020-02-07\t🇮🇳IND\t108,729,519\t82,516,220\t14,822,146\t7,331.73\n' +
  '2020-02-07\t🇮🇩IDN\t45,643,200\t30,418,489\t9,875,513\t4,310.31\n' +
  '2020-02-07\t🇺🇸USA\t11,077,553\t8,136,135\t2,648,581\t1,642.59\n' +
  '2020-02-07\t🇲🇽MEX\t14,528,993\t5,563,394\t9,609,173\t1,368.15\n' +
  '2020-02-06\t🇮🇳IND\t97,473,061\t72,822,512\t13,914,699\t7,767.29\n' +
  '2020-02-06\t🇮🇩IDN\t45,910,362\t31,896,621\t9,034,336\t4,429.01\n' +
  '2020-02-06\t🇺🇸USA\t8,843,986\t6,350,570\t2,283,220\t1,542.55\n' +
  '2020-02-06\t🇲🇽MEX\t14,188,699\t5,167,684\t9,709,603\t1,261.89\n' +
  '2020-02-05\t🇮🇳IND\t105,576,449\t78,935,915\t11,724,875\t7,768.52\n' +
  '2020-02-05\t🇮🇩IDN\t44,544,351\t31,233,636\t7,962,583\t5,031.96\n' +
  '2020-02-05\t🇺🇸USA\t9,412,303\t7,060,658\t2,081,805\t803.59\n' +
  '2020-02-05\t🇲🇽MEX\t15,453,994\t6,466,243\t9,506,465\t1,209.81\n' +
  '2020-02-04\t🇮🇳IND\t97,244,160\t74,794,269\t11,431,876\t7,408.19\n' +
  '2020-02-04\t🇮🇩IDN\t35,312,800\t30,566,249\t437,434\t4,970.63\n' +
  '2020-02-04\t🇲🇽MEX\t15,358,859\t5,907,177\t8,717,379\t1,233.60\n' +
  '2020-02-04\t🇺🇸USA\t7,251,264\t4,995,146\t2,086,367\t692.04'

const info = options => {
  console.log('options', options)
  const body = JSON.parse(options.body)

  let data = text.split('\n').slice(1).map(row => {
    const s = row.replace(/,/g, '').split('\t')
    return {
      day: s[0],
      country: s[1].substr(4, 2),
      waterfallRequest: Math.round(Number(s[2]) * Math.random() * 5),
      waterfallFilled: Math.round(Number(s[2]) * 0.7),
      instanceRequest: Math.round(Number(s[2]) * Math.random() * 5),
      instanceFilled: Math.round(Number(s[2]) * 0.6),
      click: Math.round(Number(s[3]) * Math.random() * 5),
      apiClick: Math.round(Number(s[3]) * 0.9),
      impr: Math.round(Number(s[4]) * Math.random() * 5),
      apiImpr: Math.round(Number(s[4]) * 0.8 * Math.random() * 5),
      revenue: Number(s[5]) * Math.random() * 5,
      dau: Math.round(Number(s[2]) * Math.random() * 5),
      deu: Math.round(Number(s[2]) * 0.7)
    }
  })

  const { dimension } = body
  if (dimension.length === 1) {
    const keyMap = {}
    // drop conflict dimension
    data = data.filter(d => {
      const key = d[dimension[0]]
      if (keyMap[key]) {
        return false
      } else {
        keyMap[key] = true
        return true
      }
    })
  }

  return builder(data)
}

const headRevenues = options => {
  console.log('options', options)
  return builder({
    yesterdayRevenue: 234.4,
    lastSevenDayRevenue: 452.3,
    currentMonthRevenue: 3210.45,
    lastMonthRevenue: 1235.22
  })
}

const regionsRevenues = options => {
  console.log('options', options)
  const countryMap = {}
  const data = text.split('\n').slice(1).map(row => {
    const s = row.replace(/,/g, '').split('\t')
    let country = s[1].substr(4)
    if (Object.keys(countryMap).length < 3) {
      countryMap[country] = true
    } else if (!countryMap[country]) {
      country = 'OTHER'
    }
    return {
      day: s[0],
      country,
      revenue: Number(s[5]) * Math.random() * 5
    }
  })
  return builder(data)
}

const reportList = options => {
  console.log('options', options)
  const csv = '10\tmockTest\t10\t1\ttest@adtiming.com\t1\t1\t1\tday,adnId\trequest,filled\t'
  const data = csv.split('\n').map(row => {
    const s = row.split('\t')
    return {
      id: Number(s[0]),
      name: s[1],
      publisherId: Number(s[2]),
      dataSource: Number(s[3]),
      recipients: s[4],
      schedule: Number(s[5]),
      weeklyDay: Number(s[6]),
      dimensions: s[7],
      metrics: s[8],
      status: 0
    }
  })
  return builder(data)
}

Mock.mock(/\/api\/report\/list/, 'post', info)
Mock.mock(/\/api\/report\/dashboard\/head\/revenues/i, 'get', headRevenues)
Mock.mock(/\/api\/report\/dashboard\/regions\/revenues/i, 'get', regionsRevenues)
Mock.mock(/\/api\/report\/builders/i, 'get', reportList)
